<?php
App::uses('AppModel', 'Model');
/**
 * Company Model
 *
 * @property Order $Order
 * @property User $User
 */
class Company extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Order' => array(
			'className' => 'Order',
			'foreignKey' => 'company_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'User' => array(
			'className' => 'User',
			'joinTable' => 'companies_users',
			'foreignKey' => 'company_id',
			'associationForeignKey' => 'user_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
		)
	);

 /*
  * Find Query 
 */
        
        public function getCompaniesByUserId($userId = null) {
                if(empty($userId)) return false;
                $companies = $this->find('list', array(
                    'joins' => array(
                        array('table' => 'companies_users',
                            'alias' => 'UserCompany',
                            'type' => 'INNER',
                            'conditions' => array(
                                'UserCompany.user_id' => $userId,
                                'UserCompany.company_id = Company.id'
                            )
                        )
                    ),
                    'group' => 'Company.id'
                ));
                return $companies;
            }
}
